#include "SiuRendererPTR.h"
#include "SiuCommon.h"
// Windows 头文件
#include <windows.h>
// CImage
#include <atlimage.h>



// C 运行时头文件
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>
#include <stdio.h>




SiuRendererPTR::SiuRendererPTR()
{

}

SiuRendererPTR::~SiuRendererPTR()
{

}

//
// WndProc
//
LRESULT CALLBACK WndProc(
	HWND hwnd,      // handle to window
	UINT uMsg,      // message identifier
	WPARAM wParam,  // first message parameter
	LPARAM lParam   // second message parameter
)
{
	switch (uMsg)
	{
	case WM_DESTROY:
		::PostQuitMessage(0);
		break;

	case WM_KEYDOWN:
		if (wParam == VK_ESCAPE)
			::DestroyWindow(hwnd);
		break;
	}
	return ::DefWindowProc(hwnd, uMsg, wParam, lParam);
}

void SiuRendererPTR::SetUp()
{
	printf("[SiuRendererPTR][SetUp]");

	SetPathTracer();

	return;
	WORD wHatchBmp1[] = { 0x11, 0x22, 0x44, 0x88, 0x11, 0x22, 0x44, 0x88 };
	WORD* wHatchBmp = new WORD[Height *Width];
	for (int y = 0; y < Height; y++)
	{
		for (int x = 0; x < Width; x++)
		{
			wHatchBmp[y * Width + x] = 0x99;
		}
	}

	HBITMAP hBitmap = (HBITMAP)::LoadImage(NULL,
		_T("G:\\2.bmp"),
		IMAGE_BITMAP,
		0, 0, //原始大小
		LR_DEFAULTCOLOR | LR_CREATEDIBSECTION | LR_LOADFROMFILE);
	m_hBrush = CreatePatternBrush(hBitmap);
  
	DWORD adwBitmasks[3] = { 0xffffffff, 0x000000f0, 0x00000f00 };
	CImage image;
	image.CreateEx(100, 100, 16, BI_BITFIELDS, adwBitmasks, 0);

	hBitmap = (HBITMAP)image;


	//m_hBitmap = CreateBitmap(Width, Height, 1, 8, wHatchBmp);
	m_hBrush = CreatePatternBrush(hBitmap);
	if (!SetWindows(Height, Width))
	{
		MessageBox(NULL, _T("Create New Window Failed."), _T("Fatal Error"), MB_OK);
		return;
	}
}

bool SiuRendererPTR::SetPathTracer()
{
	printf("[SiuRendererPTR][SetPathTracer]");

	m_pPathTracer = new PathTracer();
	m_pPathTracer->Render();
	
	return true;
}


bool SiuRendererPTR::SetWindows(int height, int width)
{
	HINSTANCE hInstance = NULL;
	if (hInstance == NULL)
		hInstance = (HINSTANCE)GetModuleHandle(NULL);


	HICON hIcon = NULL;

	// Register the windows class
	WNDCLASS wndClass;
	wndClass.style = CS_DBLCLKS;
	wndClass.lpfnWndProc = (WNDPROC)WndProc;

	wndClass.cbClsExtra = 0;
	wndClass.cbWndExtra = 0;
	wndClass.hInstance = hInstance;
	wndClass.hIcon = hIcon;
	wndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
	wndClass.hbrBackground = m_hBrush;
	wndClass.lpszMenuName = NULL;
	wndClass.lpszClassName = _T("SiuRenderLabWindowCalss");

	if (!RegisterClass(&wndClass))
	{
		DWORD dwError = GetLastError();
		if (dwError != ERROR_CLASS_ALREADY_EXISTS)
			OutputDebugString(_T("Cannot Register Window Class."));
		return false;
	}
	// Create the render window
	m_hWnd = CreateWindow(_T("SiuRenderLabWindowCalss"), _T("SuiRenderLab"), WS_OVERLAPPEDWINDOW,
		0, 0, width, height, 0,
		NULL, hInstance, 0);

	RECT realRect;
	GetClientRect(m_hWnd, &realRect);

	int realwidth = realRect.right - realRect.left;
	int realheight = realRect.bottom - realRect.top;
	width = width * 2 - realwidth;
	height = height * 2 - realheight;

	MoveWindow(m_hWnd, GetSystemMetrics(SM_CXSCREEN) / 2 - width / 2, GetSystemMetrics(SM_CYSCREEN) / 2 - height / 2, width, height, FALSE);

	ShowWindow(m_hWnd, SW_SHOWNORMAL);
	SetFocus(m_hWnd);
	SetForegroundWindow(m_hWnd);

	return true;
}


void SiuRendererPTR::CleanUp()
{
    DeleteObject (m_hBitmap);
	delete m_pPathTracer;
}

void SiuRendererPTR::Update()
{

}
